package com.shawn.baseappbygroovydsl.ui.view.cash

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.content.Context
import android.content.res.Resources
import android.view.View
import android.view.animation.AccelerateDecelerateInterpolator
import android.view.animation.AccelerateInterpolator
import android.view.animation.Animation
import android.view.animation.AnimationUtils
import android.view.animation.OvershootInterpolator
import com.airbnb.lottie.LottieAnimationView
import com.choryan.quan.videowzproject.extension.ExtensionView.show

object  UtilAnime {

    fun close(vararg views: View, animationFinish: () -> Unit) {
        val animatorSet = AnimatorSet()
        views.forEach { view ->
            val scaleUpX = ObjectAnimator.ofFloat(view, "scaleX", 1f, 1.1f)
            val scaleUpY = ObjectAnimator.ofFloat(view, "scaleY", 1f, 1.1f)
            val scaleDownX = ObjectAnimator.ofFloat(view, "scaleX", 1.1f, 1f)
            val scaleDownY = ObjectAnimator.ofFloat(view, "scaleY", 1.1f, 1f)
            animatorSet.play(scaleUpX).with(scaleUpY) // 放大动画
            animatorSet.play(scaleDownX).with(scaleDownY).after(scaleUpX) // 缩小动画
        }
        animatorSet.interpolator = OvershootInterpolator(2f)
        animatorSet.duration = 300
        animatorSet.addListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: android.animation.Animator) {
                super.onAnimationEnd(animation)
                animationFinish()
            }
        })
        animatorSet.start()
    }

    fun animateViewRotation(view: View,  callback: (() -> Unit)? = null) {
        val rotationAnimator = ObjectAnimator.ofFloat(view, "rotation", 0f, 1080f)
        rotationAnimator.duration = 500
        rotationAnimator.addListener(object : Animator.AnimatorListener {
            override fun onAnimationStart(animation: Animator) {
            }

            override fun onAnimationEnd(animation: Animator) {
                callback?.invoke()
            }

            override fun onAnimationCancel(animation: Animator) {
            }

            override fun onAnimationRepeat(animation: Animator) {
            }
        })

        // 启动动画
        rotationAnimator.start()
    }

    fun rotateViewVertically(view: View, onAnimationEnd: () -> Unit) {
        val rotationAnimator = ObjectAnimator.ofFloat(view, "rotationY", 0f, 1080f)
        rotationAnimator.addListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: android.animation.Animator) {
                super.onAnimationEnd(animation)
                onAnimationEnd()
            }
        })
        rotationAnimator.duration = 2000
        rotationAnimator.start()
    }
    fun applyExitAnimation(view: View, animeResId: Int, onAnimationEnd: () -> Unit) {
        val animation = AnimationUtils.loadAnimation(view.context, animeResId)
        animation.setAnimationListener(object : Animation.AnimationListener {
            override fun onAnimationStart(animation: Animation?) {
            }
            override fun onAnimationEnd(animation: Animation?) {
                onAnimationEnd()
            }
            override fun onAnimationRepeat(animation: Animation?) {
            }
        })

        view.startAnimation(animation)
    }

    fun startDualSlideAnimation(
        context: Context,
        leftView: View,
        rightView: View,
        duration: Long = 1000,
        listener: (() -> Unit)? = null
    ) {
        val screenWidth = context.resources.displayMetrics.widthPixels

        val leftStartX =0f
        val leftEndX = screenWidth.toFloat()-leftView.width.toFloat()

        val leftAnimator = ObjectAnimator.ofFloat(leftView, "translationX", leftStartX, leftEndX).apply {
            this.duration = duration
            this.interpolator= AccelerateInterpolator()
        }


        val rightStartX = 0f
        val rightEndX =  -(screenWidth.toFloat()-rightView.width.toFloat())

        val rightAnimator = ObjectAnimator.ofFloat(rightView, "translationX", rightStartX, rightEndX).apply {
            this.duration = duration
            this.interpolator= AccelerateInterpolator()
        }
        val animatorSet = AnimatorSet().apply {
            playTogether(rightAnimator,leftAnimator)
            addListener(object : Animator.AnimatorListener {
                override fun onAnimationStart(animation: Animator) {}

                override fun onAnimationEnd(animation: Animator) {
                    listener?.invoke()
                }

                override fun onAnimationCancel(animation: Animator) {}

                override fun onAnimationRepeat(animation: Animator) {}
            })
        }
        animatorSet.start()
    }

    fun viewBack(view2: View){
        val screenHeight = Resources.getSystem().displayMetrics.heightPixels
        val animator2 = ObjectAnimator.ofFloat(
            view2,
            "translationY",
            0f,
            -view2.height.toFloat() - screenHeight.toFloat()
        )
        animator2.addListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator) {
                super.onAnimationEnd(animation)
                view2.translationY = 0f
                view2.show()
            }
        })

        animator2.start()
    }
    fun animateViews(view1: View, view2: View,view3: View, onAnimationEnd: () -> Unit) {
        view1.visibility = View.VISIBLE
        view1.alpha = 0f
        view1.scaleX = 0f
        view1.scaleY = 0f
        view1.pivotX = view1.width / 2f
        view1.pivotY = view1.height / 2f
        view2.visibility = View.VISIBLE
        val alphaAnimator1 = ObjectAnimator.ofFloat(view1, "alpha", 0f, 1f)
        val scaleXAnimator1 = ObjectAnimator.ofFloat(view1, "scaleX", 0f, 1f)
        val scaleYAnimator1 = ObjectAnimator.ofFloat(view1, "scaleY", 0f, 1f)
        alphaAnimator1.duration = 1000
        scaleXAnimator1.duration = 1000
        scaleYAnimator1.duration = 1000
        alphaAnimator1.interpolator = AccelerateDecelerateInterpolator()
        scaleXAnimator1.interpolator = AccelerateDecelerateInterpolator()
        scaleYAnimator1.interpolator = AccelerateDecelerateInterpolator()
        val screenHeight = Resources.getSystem().displayMetrics.heightPixels
        val animator2 = ObjectAnimator.ofFloat(
            view2,
            "translationY",
            0f,
            -view2.height.toFloat() - screenHeight.toFloat()
        )
        animator2.duration=1000
        view3.visibility = View.VISIBLE
        view3.alpha = 1f
        view3.scaleY = 0f
        view3.pivotY = view3.height.toFloat()
        val scaleYAnimator3 = ObjectAnimator.ofFloat(view3, "scaleY", 0f, 1f)
        scaleYAnimator3.duration = 300
        scaleYAnimator3.interpolator = AccelerateDecelerateInterpolator()
        scaleYAnimator3.startDelay=300
        val animatorSet = AnimatorSet()
        animatorSet.playTogether(alphaAnimator1, scaleXAnimator1, scaleYAnimator1, animator2,scaleYAnimator3)
        animatorSet.addListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator) {
                super.onAnimationEnd(animation)
                onAnimationEnd()
            }
        })
        animatorSet.start()
    }

     fun startLav(lav: LottieAnimationView, onAnimationEnd: (() -> Unit)? = null){
        lav.addAnimatorListener(object : Animator.AnimatorListener {
            override fun onAnimationStart(animation: Animator) {
            }
            override fun onAnimationEnd(animation: Animator) {
                lav.removeAllAnimatorListeners()
                onAnimationEnd?.invoke()
            }

            override fun onAnimationCancel(animation: Animator) {
            }

            override fun onAnimationRepeat(animation: Animator) {
            }

        })
        lav.post{
            val parentWidth = lav.width
            lav.layoutParams.width = parentWidth
            lav.playAnimation()
        }
    }
}